from pydantic import BaseModel, Field
from typing import List, Optional
from datetime import datetime

from services.server_integration.schemas.modem_configuration import Modem
from services.server_integration.schemas.unit_configuration import UnitConfiguration
from services.server_integration.schemas.relay_channel_configuration import RelayChannel
from services.server_integration.schemas.service_channel_configuration import ServiceChannel
from services.server_integration.schemas.alarm_configuration import AlarmConfig
from settings import settings


class FullUnitConfiguration(BaseModel):
    unit_configuration: Optional[UnitConfiguration] = None  # UnitConfiguration()

    relay_channels: Optional[List[RelayChannel]] = None  # [RelayChannel(index=idx) for idx in range(settings.SERVICES_VIRTUAL_RELAYS_NUMBER)]
    service_channels: Optional[List[ServiceChannel]] = None  # [ServiceChannel(index=idx) for idx in range(settings.SERVICES_MODULE_NUMBER)]

    alarm_config: Optional[AlarmConfig] = None  # AlarmConfig()

    modem: Optional[Modem] = None  # Modem()
    time: datetime = Field(default_factory=lambda: datetime.utcnow())

    def set_rm_enable(self, index: int, state: bool):
        for i in range(settings.SERVICES_VIRTUAL_RELAYS_NUMBER):
            self.relay_channels[i].set_rm_enable(index, state)

    def set_optic_enable(self, index: int, state: bool):
        for i in range(settings.SERVICES_VIRTUAL_RELAYS_NUMBER):
            self.relay_channels[i].set_optic_enable(index, state)

    def set_rm_central_frequency(self, index: int, ul: float = None, dl: float = None):
        for i in range(settings.SERVICES_VIRTUAL_RELAYS_NUMBER):
            self.relay_channels[i].set_rm_central_frequency(index, ul, dl)

    def set_rm_input_gain(self, index: int, gain: float):
        for i in range(settings.SERVICES_VIRTUAL_RELAYS_NUMBER):
            self.relay_channels[i].set_rm_input_gain(index, gain)

    def set_rm_output_gain(self, index: int, gain: float):
        for i in range(settings.SERVICES_VIRTUAL_RELAYS_NUMBER):
            self.relay_channels[i].set_rm_output_gain(index, gain)

    def set_relay_enabled(self, index: int, ul: bool = None, dl: bool = None):
        self.relay_channels[index].set_enabled(ul, dl)

    def set_relay_bandwidth(self, index: int, ul: float = None, dl: float = None):
        self.relay_channels[index].set_bandwidth(ul, dl)

    def set_relay_frequency_offset(self, index: int, ul: float = None, dl: float = None):
        self.relay_channels[index].set_frequency_offset(ul, dl)


